//------------------------------------------------
//              SPI.c
//      Arthur Benemann 19/12/2011
//------------------------------------------------
#include "picDev.h"

#define SPIBUF  SPI2BUF
#define SPICON  SPI2CON1
#define SPISTAT SPI2STAT
#define SPIRFUL SPI2STATbits.SPIRBF

void initSPI1() {
    // init the spi module for a slow (safe) clock speed first
    SPICON = 0x0360; // CKE=1; CKP=1, sample middle,  1:4
    SPISTAT = 0x8000; // enable the peripheral
}
// send one byte of data and receive one back at the same time
unsigned char writeSPI(unsigned char b) {
    SPIBUF = b; // write to buffer for TX
    while (!SPIRFUL); // wait transfer complete
    return SPIBUF; // read the received value
}
